package com.tianqu.tms.www.finance.controller;

import java.util.Date;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.dto.TransferFinanceDetailDto;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferFinance;
import com.tianqu.tms.entity.TransferProcessHistory;
import com.tianqu.tms.www.finance.actionform.CarProxyPaySearchInputForm;

public class CarProxyPayTransferResultController extends TmsController {
	private Logger logger = Logger.getLogger(CarProxyPayTransferResultController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		try {
			controller.begin();
			TmsUserProfile profile = uc.getUserProfile();

			CarProxyPaySearchInputForm inputForm = (CarProxyPaySearchInputForm) uc.getInputForm();
			//
			if(inputForm.getTransferPriceChargeType() != 15 && inputForm.getTransferPriceChargeType() != 16) {//15:抵押登记   //16:原车主地址变更
				TransferAcceptedInfo transferAcceptedInfo = TransferAcceptedInfo.getInstance();
				transferAcceptedInfo.updateTransferPriceBySerial(Integer.parseInt(inputForm.getSaveType()),inputForm.getTransferPriceChargeType(),inputForm.getSerial());
			}
			
			//
			TransferProcessHistory process = new TransferProcessHistory();
			process.setSerial(inputForm.getSerial());
			if("1".equals(inputForm.getSaveType())) process.setBusinessType(30);// 30：过户综合收费
			else if("2".equals(inputForm.getSaveType())) process.setBusinessType(31);// 30：过户退费
			Codes code = Codes.getInstance();
			if("1".equals(inputForm.getSaveType())) process.setReason(code.getItemValueByItemIdAndItemCode(Codes.CHARGE_TYPE_CHARGE, inputForm.getTransferPriceChargeType()));
			else if("2".equals(inputForm.getSaveType())) process.setReason(code.getItemValueByItemIdAndItemCode(Codes.CHARGE_TYPE_REFUND, inputForm.getTransferPriceChargeType()));
			process.setInsertUserId(profile.getUniqueId());
			process.insertHisInfoAtAssess(process);

			//
			TransferFinanceDetailDto transferFinanceDetailDto = new TransferFinanceDetailDto();
			transferFinanceDetailDto.setSerial(inputForm.getSerial());
			transferFinanceDetailDto.setPriceType(inputForm.getTransferPriceChargeType());
			transferFinanceDetailDto.setPriceChargeType(1);// 现金
			if("1".equals(inputForm.getSaveType())) transferFinanceDetailDto.setPriceWay(1);// 收费
			else transferFinanceDetailDto.setPriceWay(2);// 退费
			transferFinanceDetailDto.setPrice(Integer.parseInt(StringUtil.removeDH(inputForm.getPrice())));
			transferFinanceDetailDto.setPriceDate(DateUtil.getCurrentTime());
			transferFinanceDetailDto.setPriceUserId(profile.getUniqueId());
			transferFinanceDetailDto.setPriceRemark(inputForm.getRemarks());
			if(inputForm.getTransferPriceChargeType() != 9) transferFinanceDetailDto.setAcceptedtime(new Date());
			TransferFinance transferFinance = new TransferFinance();
			transferFinance.insertTransferFinanceDetail(transferFinanceDetailDto);

			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				try {
					throw e;
				} catch (Exception e1) {
				}
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");

	}
}
